package com.actor.java;

import com.bean.ResultBean;
import com.util.FileUtil;
import com.util.StringUitl;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.IOException;
import java.io.StringWriter;

/**
 * @Description
 * @Author semstouch
 * @Date 2017/6/28
 **/
public class ManageBeanActor {
    /**
     * @param args
     */
    public ManageBeanActor(ResultBean resultBean) {
        try {
            Velocity.init();
        } catch (Exception e) {
            System.out.println("创建action模版失败" + e);
            return;
        }
        String tableName=resultBean.getTableBean().getTableName().split("_")[1];
        String objectComment = resultBean.getTableBean().getTableComment();
        String objectName = StringUitl.CaptureName(tableName);
        String objectParam= tableName;
        String packagePath = "shop.services.manage." + tableName+".bean";
        VelocityContext context = new VelocityContext();
        context.put("packagePath", packagePath);
        context.put("crateTime", StringUitl.GetNowDate());
        context.put("objectName", objectName);
        context.put("objectParam", objectParam);
        context.put("objectComment", objectComment.replaceAll("表", ""));
        StringWriter w = new StringWriter();


        try {
            Velocity.mergeTemplate("src/main/java/com/template/java/managebean.vm", "UTF-8", context, w);
        } catch (Exception e) {
            System.out.println("模版匹配失败: " + e);
        }

        try {
            FileUtil.creatTxtFile(packagePath, objectName, ".java", w.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
